package com.example.networklibrary;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * Created by zhuangdaoyuan on 2018/7/9.
 * 安静撸码，淡定做人
 */
public class MD5 {
    private static String MD5(String plaintext, String charset) throws UnsupportedEncodingException {
        String str = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plaintext.getBytes(charset));
            byte[] b = md.digest();
            StringBuffer buf = new StringBuffer("");

            for (int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            str = buf.toString();
            return str;
        } catch (NoSuchAlgorithmException var8) {
            var8.printStackTrace();
            return "";
        }
    }

    public static String getSign(String json) throws UnsupportedEncodingException {
        json = json.toLowerCase();
        char[] c = json.toCharArray();
        Arrays.sort(c);
        String ptext = MD5(new String(c) + MD5("zhiyis", "utf-8"), "utf-8");
        return ptext;
    }
}
